Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports NENAData.LINQ
Imports NENAData.NENAProvinces
Imports NENAData.NENAMember
Imports NENAData.DLC

Partial Class UpdateMyDemographics
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtNurseLicense As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then

            txtMemberID.Text = Session("Member_ID")
            Load_Province_DDL()
            'load_ENAODistricts_DDL()
            BindMemberData()
        End If

        If ddlPersonalProvince.SelectedItem.Text.ToString = "Other" Then
            lblOtherState.Visible = True
            txtOtherState.Visible = True
            lblOtherCountry.Visible = True
            txtOtherCountry.Visible = True
        Else
            lblOtherState.Visible = False
            txtOtherState.Visible = False
            lblOtherCountry.Visible = False
            txtOtherCountry.Visible = False
        End If

        If ddlPersonalProvince.SelectedValue > 0 Then
            ddlAffiliate.Visible = True
            lblAffiliate.Visible = True
            lblFee.Visible = True
            'load the affiliate ddl according to the selected province
            'only execute the affiliate ddl when the province changes
            If txtProvinceID.Text <> ddlPersonalProvince.SelectedValue.ToString Then
                Load_Affiliate_DDL()
            End If
            'update the label to display the proper fee
            Show_Fee()
        Else
            ddlAffiliate.Visible = False
            lblAffiliate.Visible = False
            lblFee.Visible = False
        End If
        txtProvinceID.Text = ddlPersonalProvince.SelectedValue.ToString

        'If ddlPersonalProvince.SelectedValue.ToString = "5" Then
        '    lbl_ENAODistricts.Visible = True
        '    rv_ENAODistricts.Visible = True
        '    ddl_ENAODistricts.Visible = True
        '    check_RNAO.Visible = True
        'Else
        '    lbl_ENAODistricts.Visible = False
        '    rv_ENAODistricts.Visible = False
        '    ddl_ENAODistricts.Visible = False
        '    check_RNAO.Visible = False
        'End If

    End Sub

    Private Sub BindMemberData()
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql, strMemberID As String

        strMemberID = Session("Member_ID")

        strSql = "select * from members, Status, affiliate where " & _
       "members.member_id =" & strMemberID & _
       " and members.affiliate_id = affiliate.affiliate_id " & _
       "and members.Account_Status = Status.Status_ID"

        conPubs = New SqlConnection(ConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString.ToString)
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        While dtrTitles.Read
            PersonalFirstName.Text = dtrTitles("FirstName")
            PersonalLastName.Text = dtrTitles("LastName")
            PersonalEmail.Text = dtrTitles("emailaddress")
            PersonalHomePhone.Text = dtrTitles("homephone")
            PersonalAddress1.Text = dtrTitles("address")
            PersonalAddress2.Text = dtrTitles("address2")
            PersonalCity.Text = dtrTitles("city")
            PersonalPostalCode.Text = dtrTitles("postalcode")
            ddlPersonalProvince.SelectedIndex = dtrTitles("affiliate_id")

            'If dtrTitles("affiliate_id") = "5" Then
            'ddl_ENAODistricts.SelectedIndex = dtrTitles("ENAODistrict_ID") - 1
            'End If

            txtOtherCountry.Text = dtrTitles("country")
            txtWorkPhone.Text = dtrTitles("workphone")
            txtEmployer.Text = dtrTitles("employer")
            txtStatus.Text = dtrTitles("statusdescription")
            'PersonalFirstName.Text = dtrTitles("FirstName")

            chkENPCProvider.Checked = ReturnChecked(dtrTitles("enpc_provider"))
            chkENPCInstructor.Checked = ReturnChecked(dtrTitles("enpc_instructor"))
            chkENPCTrainer.Checked = ReturnChecked(dtrTitles("enpc_course_trainer"))
            chkENPCDirector.Checked = ReturnChecked(dtrTitles("enpc_course_director"))
            check_RNAO.Checked = ReturnChecked(dtrTitles("RNAO"))

            txtENPCID.Text = dtrTitles("enpc_trainer_id")
            If chkENPCInstructor.Checked Then
                lblENPCID.Visible = True
                txtENPCID.Visible = True
                ValidateENPCID.Enabled = True
                ValidateENPCID.Visible = True
            End If

            chkPCTASProvider.Checked = ReturnChecked(dtrTitles("pctas_provider"))
            chkPCTASInstructor.Checked = ReturnChecked(dtrTitles("pctas_instructor"))
            chkPCTASTrainer.Checked = ReturnChecked(dtrTitles("pctas_course_trainer"))
            chkPCTASDirector.Checked = ReturnChecked(dtrTitles("pctas_course_director"))

            chkACTASProvider.Checked = ReturnChecked(dtrTitles("actas_provider"))
            chkACTASInstructor.Checked = ReturnChecked(dtrTitles("actas_instructor"))
            chkACTASTrainer.Checked = ReturnChecked(dtrTitles("actas_course_trainer"))
            chkACTASDirector.Checked = ReturnChecked(dtrTitles("actas_course_director"))

            chkCCTASTrainer.Checked = ReturnChecked(dtrTitles("CCTAS_Provider"))
            chkCCTASProvider.Checked = ReturnChecked(dtrTitles("cctas_Trainer"))

            chkCATNProvider.Checked = ReturnChecked(dtrTitles("catn_provider"))
            chkCATNInstructor.Checked = ReturnChecked(dtrTitles("catn_instructor"))
            chkCATNTrainer.Checked = ReturnChecked(dtrTitles("catn_course_trainer"))
            chkCATNDirector.Checked = ReturnChecked(dtrTitles("catn_course_director"))

            chkTNCCProvider.Checked = ReturnChecked(dtrTitles("tncc_provider"))
            chkTNCCInstructor.Checked = ReturnChecked(dtrTitles("tncc_instructor"))
            chkTNCCTrainer.Checked = ReturnChecked(dtrTitles("tncc_course_trainer"))
            chkTNCCDirector.Checked = ReturnChecked(dtrTitles("tncc_course_director"))
            txtTNCCID.Text = dtrTitles("tncc_trainer_id")
            If chkTNCCInstructor.Checked Then
                lblTNCCID.Visible = True
                txtTNCCID.Visible = True
                validateTNCCID.Enabled = True
                validateTNCCID.Visible = True
            End If

            chkENCCCertified.Checked = ReturnChecked(dtrTitles("encc_certified"))
            chkEmployeeAdministration.Checked = ReturnChecked(dtrTitles("aoe_administration"))
            chkEmployeeClinical.Checked = ReturnChecked(dtrTitles("aoe_clinical"))
            chkEmployeeEducator.Checked = ReturnChecked(dtrTitles("aoe_educator"))
            chkEmployeeResearch.Checked = ReturnChecked(dtrTitles("aoe_research"))

            txtMemberID.Text = dtrTitles("member_id")
            txtNurseLicenseID.Text = dtrTitles("Nurses_License_ID")
            txtEmployer.Text = dtrTitles("Employer")
            txtMembershipReferralCode.Text = dtrTitles("Referral_Code")

            If txtMembershipReferralCode.Text.Length < 2 Then
                'All paid members get a referral code created
                btnGenerateReferralCode.Visible = True
            End If

        End While
        dtrTitles.Close()
        conPubs.Close()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleNoSave")) Then
            Response.Redirect("default.aspx")
        End If

        Dim strFirstName, strLastName, strEmail, strHomePhone As String
        Dim strAddress1, strAddress2, strCity, strPostalCode As String
        Dim strAffiliateId, strEmployer As String
        Dim strProvince, strCountry, strWorkPhone As String
        Dim strAdministration, strClinical, strEducator, strResearch As String
        Dim strPCTASProvider, strPCTASInstructor, strPCTASTrainer, strPCTASDirector As String
        Dim strACTASProvider, strACTASInstructor, strACTASTrainer, strACTASDirector As String
        Dim strCCTASProvider, strCCTASTrainer As String
        Dim strENPCProvider, strENPCInstructor, strENPCTrainer, strENPCDirector As String
        Dim strCATNProvider, strCATNInstructor, strCATNTrainer, strCATNDirector As String
        Dim strTNCCProvider, strTNCCInstructor, strTNCCTrainer, strTNCCDirector As String
        Dim strSQL, strENCCCertified, strNursesLicenseID As String
        Dim strENPCID, strTNCCID, strRNAO As String

        If txtMemberID.Text = "0" Then
            Exit Sub
        End If

        strENPCID = SqlReplaceText(txtENPCID.Text)
        strTNCCID = SqlReplaceText(txtTNCCID.Text)

        strFirstName = SqlReplaceText(PersonalFirstName.Text)
        strLastName = SqlReplaceText(PersonalLastName.Text)
        strEmail = SqlReplaceText(PersonalEmail.Text)
        strHomePhone = SqlReplaceText(PersonalHomePhone.Text)
        strAddress1 = SqlReplaceText(PersonalAddress1.Text)
        strAddress2 = SqlReplaceText(PersonalAddress2.Text)
        strCity = SqlReplaceText(PersonalCity.Text)
        strPostalCode = SqlReplaceText(PersonalPostalCode.Text)
        strAffiliateId = ddlAffiliate.SelectedValue.ToString
        strProvince = ddlPersonalProvince.SelectedItem.Text
        strCountry = SqlReplaceText(txtOtherCountry.Text)
        strWorkPhone = SqlReplaceText(txtWorkPhone.Text)
        strEmployer = SqlReplaceText(txtEmployer.Text)

        strAdministration = EvaluateChecked(chkEmployeeAdministration.Checked)
        strClinical = EvaluateChecked(chkEmployeeClinical.Checked)
        strEducator = EvaluateChecked(chkEmployeeEducator.Checked)
        strResearch = EvaluateChecked(chkEmployeeResearch.Checked)

        strENCCCertified = EvaluateChecked(chkENCCCertified.Checked)

        strPCTASProvider = EvaluateChecked(chkPCTASProvider.Checked)
        strPCTASInstructor = EvaluateChecked(chkPCTASInstructor.Checked)
        strPCTASTrainer = EvaluateChecked(chkPCTASTrainer.Checked)
        strPCTASDirector = EvaluateChecked(chkPCTASDirector.Checked)

        strACTASProvider = EvaluateChecked(chkACTASProvider.Checked)
        strACTASInstructor = EvaluateChecked(chkACTASInstructor.Checked)
        strACTASTrainer = EvaluateChecked(chkACTASTrainer.Checked)
        strACTASDirector = EvaluateChecked(chkACTASDirector.Checked)

        strCCTASProvider = EvaluateChecked(chkCCTASProvider.Checked)
        strCCTASTrainer = EvaluateChecked(chkACTASTrainer.Checked)

        strENPCProvider = EvaluateChecked(chkENPCProvider.Checked)
        strENPCInstructor = EvaluateChecked(chkENPCInstructor.Checked)
        strENPCTrainer = EvaluateChecked(chkENPCTrainer.Checked)
        strENPCDirector = EvaluateChecked(chkENPCDirector.Checked)

        strCATNProvider = EvaluateChecked(chkCATNProvider.Checked)
        strCATNInstructor = EvaluateChecked(chkCATNInstructor.Checked)
        strCATNTrainer = EvaluateChecked(chkCATNTrainer.Checked)
        strCATNDirector = EvaluateChecked(chkCATNDirector.Checked)

        strTNCCProvider = EvaluateChecked(chkTNCCProvider.Checked)
        strTNCCInstructor = EvaluateChecked(chkTNCCInstructor.Checked)
        strTNCCTrainer = EvaluateChecked(chkTNCCTrainer.Checked)
        strTNCCDirector = EvaluateChecked(chkTNCCDirector.Checked)

        strRNAO = EvaluateChecked(check_RNAO.Checked)

        strNursesLicenseID = txtNurseLicenseID.Text
        strEmployer = SqlReplaceText(txtEmployer.Text)

        strSQL = "update members set " & _
            "lastname = '" & strLastName & "', " & _
            "firstname = '" & strFirstName & "', " & _
            "address = '" & strAddress1 & "', " & _
            "address2 = '" & strAddress2 & "', " & _
            "city = '" & strCity & "', " & _
            "postalcode = '" & strPostalCode & "', " & _
            "province = '" & strProvince & "', " & _
            "affiliate_id = '" & strAffiliateId & "', " & _
            "country = '" & strCountry & "', "

        'default district to unknown as districts are disabled
        If ddlPersonalProvince.SelectedValue.ToString = "5" Then
            strSQL &= "ENAODistrict_ID = '" & 12 & "', "
        End If

        strSQL &= "homephone = '" & strHomePhone & "', " & _
            "workphone = '" & strWorkPhone & "', " & _
            "emailaddress = '" & strEmail & "', " & _
            "aoe_Administration = '" & strAdministration & "', " & _
            "aoe_clinical = '" & strClinical & "', " & _
            "aoe_educator = '" & strEducator & "', " & _
            "aoe_research = '" & strResearch & "', " & _
            "ENCC_Certified = '" & strENCCCertified & "', "
        strSQL &= "tncc_provider = '" & strTNCCProvider & "', " & _
            "tncc_instructor = '" & strTNCCInstructor & "', " & _
            "tncc_course_trainer = '" & strTNCCTrainer & "', " & _
            "tncc_course_director = '" & strTNCCDirector & "', " & _
            "enpc_provider = '" & strENPCProvider & "', " & _
            "enpc_instructor = '" & strENPCInstructor & "', " & _
            "enpc_course_trainer = '" & strENPCTrainer & "', " & _
            "enpc_course_director = '" & strENPCDirector & "', " & _
            "catn_provider = '" & strCATNProvider & "', " & _
            "catn_instructor = '" & strCATNInstructor & "', " & _
            "catn_course_trainer = '" & strCATNTrainer & "', " & _
            "catn_course_director = '" & strCATNDirector & "', "
        strSQL &= "pctas_provider = '" & strPCTASProvider & "', " & _
            "pctas_instructor = '" & strPCTASInstructor & "', " & _
            "pctas_course_trainer = '" & strPCTASTrainer & "', " & _
            "pctas_course_director = '" & strPCTASDirector & "', "
        strSQL &= "actas_provider = '" & strACTASProvider & "', " & _
            "actas_instructor = '" & strACTASInstructor & "', " & _
            "actas_course_trainer = '" & strACTASTrainer & "', " & _
            "actas_course_director = '" & strACTASDirector & "', " & _
            "cctas_provider = '" & strCCTASProvider & "', " & _
            "cctas_trainer = '" & strCCTASTrainer & "', "
        strSQL &= "nurses_license_id = '" & strNursesLicenseID & "', " & _
            "enpc_trainer_id = '" & strENPCID & "', " & _
            "tncc_trainer_id = '" & strTNCCID & "', " & _
            "rnao = '" & strRNAO & "', " & _
            "employer = '" & strEmployer & "' " & _
            "where member_id =" & txtMemberID.Text & ";"


        'Response.Write("strSQL: " & strSQL)

        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand

        conPubs = New SqlConnection(ConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString.ToString)
        cmdSelect = New SqlCommand(strSQL, conPubs)
        conPubs.Open()
        cmdSelect.ExecuteNonQuery()
        conPubs.Close()

        BindMemberData()
        NENAData.TCAPI.User.UpdateUser(HttpContext.Current.User.Identity.Name, PersonalEmail.Text)
        Session("Message") = "Your Profile has been successfully Updated"
        Response.Redirect("default.aspx")

    End Sub

    Private Function ReturnChecked(ByVal strChecked) As Boolean

        If strChecked = "Y" Then
            Return True
        Else
            Return False
        End If

    End Function

    Private Function EvaluateChecked(ByVal strChecked) As String

        If strChecked = "false" Then
            Return ("N")
        Else
            Return ("Y")
        End If

    End Function

    Function SqlReplaceText(ByVal strSql As String) As String
        Dim strFixedSQL As String

        strFixedSQL = Replace(strSql, "'", "")
        strFixedSQL = Replace(strFixedSQL, "[", "")
        strFixedSQL = Replace(strFixedSQL, "%", "")
        'strFixedSQL = Replace(strFixedSQL, "_", "[_]") 
        'some people may have underscores in their lastname
        strFixedSQL = Replace(strFixedSQL, "--", "")

        'return the fixed sql
        SqlReplaceText = strFixedSQL
    End Function

    Private Sub BtnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReturn.Click
        Response.Redirect("default.aspx")
    End Sub

    Private Sub Load_ENAODistricts_DDL()

        ddl_ENAODistricts.DataSource = Districts.Get_ENAODistricts()

        ddl_ENAODistricts.DataValueField = "District_ID"
        ddl_ENAODistricts.DataTextField = "District_Description" '"Province_ID"
        ddl_ENAODistricts.DataBind()
        ddl_ENAODistricts.Items.Insert(0, "--Select--")
    End Sub

    'Private Sub Load_Province_DDL()
    '   ddlPersonalProvince.DataSource = Get_Provinces_DDL()
    '  ddlPersonalProvince.DataValueField = "Province_ID"
    ' ddlPersonalProvince.DataTextField = "Province" '"Province_ID"
    'ddlPersonalProvince.DataBind()
    'End Sub

    Private Sub Load_Affiliate_DDL()
        ddlAffiliate.DataSource = Get_Affiliate_DDL()
        ddlAffiliate.DataValueField = "affiliate_id"
        ddlAffiliate.DataTextField = "affiliate_description"
        ddlAffiliate.DataBind()
    End Sub

    Function Get_Affiliate_DDL()
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select " & _
        "membership_fee, affiliate_full, affiliate_description, provinces.province, affiliate.affiliate_id " & _
        "from " & _
        "affiliate, affiliate_xref, provinces " & _
        "where  " & _
        "affiliate.affiliate_id = affiliate_xref.affiliate_id " & _
        "and affiliate_xref.province_id = provinces.province_id " & _
        "and affiliate_xref.province_id = " & ddlPersonalProvince.SelectedValue & ";"

        conPubs = New SqlConnection(ConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString.ToString)
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        'ddlProvinceProInfo.DataSource = dtrTitles
        'ddlProvinceProInfo.DataBind()
        Return dtrTitles
        dtrTitles.Close()
        conPubs.Close()
    End Function

    Private Sub Load_Province_DDL()
        Dim x As NENAProvinces = New NENAProvinces

        ddlPersonalProvince.DataSource = x.GetProvinces_All()
        ddlPersonalProvince.DataValueField = "Province_ID"
        ddlPersonalProvince.DataTextField = "ProvinceName" '"Province_ID"
        ddlPersonalProvince.DataBind()

    End Sub

    Public Sub SetAffiliate()
        If ddlPersonalProvince.SelectedValue > 0 Then
            ddlAffiliate.Visible = True
            lblAffiliate.Visible = True
            lblFee.Visible = True
            'load the affiliate ddl according to the selected province
            'only execute the affiliate ddl when the province changes
            If txtProvinceID.Text <> ddlPersonalProvince.SelectedValue.ToString Then
                Load_Affiliate_DDL()
            End If
            'update the label to display the proper fee
            SetFee()
        Else
            ddlAffiliate.Visible = False
            lblAffiliate.Visible = False
            lblFee.Visible = False
        End If
        txtProvinceID.Text = ddlPersonalProvince.SelectedValue.ToString

    End Sub

    Public Sub SetFee()

        'Show_Fee()

        If ddlPersonalProvince.SelectedValue.ToString = "5" Then
            lbl_ENAODistricts.Visible = True
            rv_ENAODistricts.Visible = True
            'ddl_ENAODistricts.Visible = True
            check_RNAO.Visible = True
        Else
            lbl_ENAODistricts.Visible = False
            rv_ENAODistricts.Visible = False
            'ddl_ENAODistricts.Visible = False
            check_RNAO.Visible = False
        End If
    End Sub

    Sub Show_Fee()

        Dim x As New NENAAffiliate
        Dim oAffiliate As Affiliate
        oAffiliate = x.GetAffiliate_byAffiliateID(CType(ddlAffiliate.SelectedItem.Value.ToString, Integer))

        lblFee.Text = "The fee for " & ddlAffiliate.SelectedItem.Text.ToString & _
                " is " & String.Format("{0:C}", oAffiliate.Membership_Fee)

        Session("payment_amt") = String.Format("{0:00.00}", oAffiliate.Membership_Fee)

        lblFee.Text = "The fee for " & ddlAffiliate.SelectedItem.Text.ToString & _
                " is " & String.Format("{0:00.00}", oAffiliate.Membership_Fee)

    End Sub

    Private Sub chkENPCInstructor_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkENPCInstructor.CheckedChanged

        'enabled ID checking
        If chkENPCInstructor.Checked = True Then
            lblENPCID.Visible = True
            txtENPCID.Visible = True
            ValidateENPCID.Enabled = True
            ValidateENPCID.Visible = True
        End If

        'disable ID checking
        If chkENPCInstructor.Checked = False Then
            lblENPCID.Visible = False
            txtENPCID.Visible = False
            ValidateENPCID.Enabled = False
            ValidateENPCID.Visible = False
        End If
    End Sub

    Private Sub chkTNCCInstructor_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTNCCInstructor.CheckedChanged

        'enabled ID checking
        If chkTNCCInstructor.Checked = True Then
            lblTNCCID.Visible = True
            txtTNCCID.Visible = True
            validateTNCCID.Enabled = True
            validateTNCCID.Visible = True
        End If

        'disable ID checking
        If chkTNCCInstructor.Checked = False Then
            lblTNCCID.Visible = False
            txtTNCCID.Visible = False
            validateTNCCID.Enabled = False
            validateTNCCID.Visible = False
        End If

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Response.Redirect("default.aspx")
    End Sub

    Protected Sub btnGenerateReferralCode_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerateReferralCode.Click
        Dim oreferral = New Referral.Generate()
        oreferral.GenerateUnqiueMemberReferralCode(Convert.ToInt32(txtMemberID.Text))
        btnGenerateReferralCode.Enabled = False
        BindMemberData()
    End Sub
End Class
